package com.tencreat.common.utils;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.ivs.v2.IvsClient;
import com.huaweicloud.sdk.ivs.v2.model.*;
import com.huaweicloud.sdk.ivs.v2.region.IvsRegion;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 实名认证
 *
 * @Author clatt
 * @Company: 深圳腾创互联科技有限公司
 * @link: https://tencreat.com
 * @Date: 2022/6/9 11:16
 */
@Slf4j
public class VerifyUtil {
    public static boolean verify(String IDCard, String name, Long uuid){
        log.info("开始实名认证：{} - {} - {}", name, IDCard,uuid);

        String ak = DictUtils.getDictValue("verify_config","ak");
        String sk = DictUtils.getDictValue("verify_config","sk");

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        IvsClient client = IvsClient.newBuilder()
                .withCredential(auth)
                .withRegion(IvsRegion.valueOf("cn-north-4"))
                .build();
        DetectExtentionByNameAndIdRequest request = new DetectExtentionByNameAndIdRequest();
        IvsExtentionByNameAndIdRequestBody body = new IvsExtentionByNameAndIdRequestBody();
        List<ExtentionReqDataByNameAndId> listIvsExtentionByNameAndIdRequestBodyDataReqData = new ArrayList<>();
        listIvsExtentionByNameAndIdRequestBodyDataReqData.add(
                new ExtentionReqDataByNameAndId()
                        .withVerificationName(name)
                        .withVerificationId(IDCard)
        );
        IvsExtentionByNameAndIdRequestBodyData databody = new IvsExtentionByNameAndIdRequestBodyData();
        databody.withReqData(listIvsExtentionByNameAndIdRequestBodyDataReqData);
        Meta metabody = new Meta();
        metabody.withUuid(uuid.toString());
        body.withData(databody);
        body.withMeta(metabody);
        request.withBody(body);
        try {
            DetectExtentionByNameAndIdResponse response = client.detectExtentionByNameAndId(request);
            List<ExtentionRespDataByNameAndId> respData = response.getResult().getRespData();
            Integer resultCode = respData.get(0).getVerificationCode();
            log.info(response.toString());
            return resultCode == 1000;
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            log.info("实名认证报错：{} - {} - {}，HttpStatusCode:{}, ErrorCode:{},ErrorMsg:{}",
                    name, IDCard,uuid,e.getHttpStatusCode(),e.getErrorCode(),e.getErrorMsg());
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        log.info("实名认证失败：{} - {} - {}",name, IDCard,uuid);
        return false;

    }
}
